﻿/**
 * The Text class handles Text boxes
*/
dynamic class Text extends MovieClip {
	
	// Constants
	var padding:Number = 10;
	
	// Variables
	var text_xml:XML;

	/**
	 * Text() initialises the Text class
	*/
	function Text() {
		
		// Initialises the text block
		this.text_xml = new XML("<text display=\"block\"></text>");

		// Attach components
		this.attachMovie("element_background", "background_mc", this.getNextHighestDepth(), {
			useHandCursor: false
		});
		
		// Create main text area
		this.createTextField("editable_txt", this.getNextHighestDepth(), 0, this.element_menu_mc._height, 100, 0);

		// Create menu
		this.attachMovie("element_menu", "element_menu_mc", this.getNextHighestDepth());
		
		// Create border
		this.attachMovie("element_border", "element_border_mc", this.getNextHighestDepth());
		
		// HTML
		this.editable_txt.html = true;
		//this.editable_txt.styleSheet = _root.text_input_ss;
		
		// HTML Text
		this.editable_txt.htmlText = "<P ALIGN=\"LEFT\"><FONT FACE=\"Verdana\" SIZE=\"12\" COLOR=\"#000000\">Waiting...</FONT></P>";
		
		// Attributes
		this.editable_txt.type = "input";
		this.editable_txt.border = false;
		this.editable_txt.wordWrap = true;
		this.editable_txt.autoSize = true;
		this.editable_txt.multiline = true;
		
		// When text field changes, redraw
		this.editable_txt.onChanged = function() {
			_parent._parent.drawLayout();
		};
		
		// When focused
		this.editable_txt.onSetFocus = function(oldFocus:Object) {
			
			// If not currently focused
			if (_parent._parent.getElementFocus() != this._parent) {
				
				// Focus on this element
				_parent._parent.setElementFocus(this._parent);
				
				// Focus on text field
				_parent._parent.setCurrentTextFocus(this);
			}
		}

		// Make background clickable
		this.background_mc.onRelease = function() {
			_parent._parent.setElementFocus(_parent);
		}
	}
	
	/**
	 * redraw redraws this element
	 *
	 * @param column_width is the column width
	*/
	function redraw(column_width:Number):Void {
		
		// Set text size
		this.editable_txt._width = column_width;
		this.editable_txt._y = this.element_menu_mc._height;
		
		// Set menu size
		this.element_menu_mc.background_mc._width = column_width;
		
		// Set background size
		this.background_mc._width = column_width;
		this.background_mc._height = this.element_menu_mc._height + this.editable_txt._height;
		
		// Set border size
		this.element_border_mc._width = column_width;
		this.element_border_mc._height = this.element_menu_mc._height + this.editable_txt._height;

		// Make sure it's visible
		this._visible = true;
		
		// Update display type
		this.displayDisplayType();
	}	
	
	/**
	 * focusElement() focuses this element
	*/
	function focusElement():Void {
		
		// Store focus
		_root.CURRENT_TEXT_FOCUS = this.editable_txt;
		
		_root.focusElement(this);
	}
	
	/**
	 * blurElement() blurs this element
	*/
	function blurElement():Void {
		
		// Remove focus to link URL box
		_root.controls_mc.link_txt.text = "";
		Selection.setFocus(_root.controls_mc.link_txt);
		
		// Remove stored focus
		_root.CURRENT_TEXT_FOCUS = null;
		
		// Blur
		_root.blurElement(this);
	}

	/**
	 * setSource() sets the source for this element
	 *
	 * @param new_text_xml is the XML to use in the text box
	*/
	function setSource(new_text_xml:String):Void {
		
		// Set variable
		this.text_xml = new XML(new_text_xml);
		
		// Update content text
		var text_content = this.text_xml.firstChild.toString();
		
		// Conver from ASCII
		text_content = unescape(_root.replaceAll("@", "%", text_content));
		
		// Set
		this.setHTMLText(text_content);
	}
	
	/**
	 * getSource() gets the source for this textbox in XML format
	*/
	function getSource():String {
		
		// Convert to ASCII
		var text_content:String = _root.toEscaped(this.editable_txt.htmlText);
		
		// Return
		return "<text display=\"" + this.text_xml.firstChild.attributes.display + "\">" + text_content + "</text>";
	}

	/**
	 * setHTMLText() sets the HTML for the text box
	 *
	 * @param html_text is the HTML text to use
	*/
	function setHTMLText(html_text:String):Void {
		
		// Set HTML Text
		this.editable_txt.htmlText = html_text;
		
		// Redraw Column
		_parent.drawLayout();
	}

	/**
	 * editLink() edits a link
	 *
	 * @link_parameters is a string consisting of a URL and a true or false for
	 *  whether it opens in a new window
	*/
	function editLink(link_parameters:String):Void {
		
		// Split
		var parameters_array:Array = link_parameters.split(",");

		// Set URL
		_root.controls_mc.link_txt.text = parameters_array[0];
		
		// Set 'Open in new window' checkbox
		_root.controls_mc.new_window_cb_mc.setCheckMode((parameters_array[1] == "true") ? 2 : 0);
	}	
	
	/**
	 * toggleDisplayType() toggles the display type
	*/
	function toggleDisplayType():Void {
		
		// If currently inline, make block
		if (this.text_xml.firstChild.attributes.display == "inline") {
			
			// Change XML
			this.text_xml.firstChild.attributes.display = "block";
			
		// vice versa
		} else {
			
			// Change XML
			this.text_xml.firstChild.attributes.display = "inline";
		}
		
		// Update
		displayDisplayType();
	}

	/**
	 * displayDisplayType() updates the display type
	*/
	function displayDisplayType():Void {
		
		// Update checkbox movieclip
		this.element_menu_mc.inline_cb_mc.setCheckMode((this.text_xml.firstChild.attributes.display == "inline") ? 0 : 2);
	}
	
	/**
	 * return the minimum width for the text box
	 *
	 * @return 280
	*/
	function getMinimumWidth():Number {
		return 280;
	}
}